from pyecharts.globals import SymbolType
from pyecharts.options import ComponentTitleOpts
from WeatherXM.service.weatherData import weather_data
import pyecharts.options as opts
from pyecharts.charts import Line, Liquid, EffectScatter, Gauge, Timeline, Scatter, Page, Grid, Pie
from pyecharts.components import Table


# # 测试数据获取
# seven_dayList, data_list, high_temperature, low_temperature, time_list, temperature_lists, \
# windspeed_lists, pressure_lists, humidity_lists, cloud_lists = weather_data('上海')

# 标题
def tab(name, color) -> Pie:  # 作为标题
    tab = (
        Pie(init_opts=opts.InitOpts(width='100%',height='100px')).
        set_global_opts(title_opts=opts.TitleOpts(title=name, pos_left='center', pos_top='center',
                                                  title_textstyle_opts=opts.TextStyleOpts(color=color, font_size=35))))
    return tab


# 需求一：当日温度变化曲线
def temperature_line(hour, temperature) -> Line:
    line = (
        Line(init_opts=opts.InitOpts(width='50%'))
        .add_xaxis(hour)
        .add_yaxis("温度", temperature, is_connect_nones=True)
        .set_series_opts(label_opts=opts.LabelOpts(formatter='{@[1]}℃'))
        .set_global_opts(title_opts=opts.TitleOpts(title="当日平均温度变化曲线"), yaxis_opts=opts.AxisOpts(
            type_='value',
            axislabel_opts=opts.LabelOpts(formatter="{value} ℃")))
    )
    return line


# 需求二：当日平均湿度
def humidity_liquid(humidity) -> Liquid():
    liquid = (
        Liquid(init_opts=opts.InitOpts(width='50%'))
        # .add("lq", [0.6, 0.7])
        .add("湿度", humidity, is_outline_show=False)
        .set_global_opts(title_opts=opts.TitleOpts(title="当日平均湿度"))
    )
    return liquid


# 需求三：当日风速变化：
def windspeed_effectScatter(hour, windspeed) -> EffectScatter:
    effectScatter = (
        EffectScatter(init_opts=opts.InitOpts(width='50%'))
        .add_xaxis(hour)
        .add_yaxis("风速", windspeed, symbol=SymbolType.ARROW)
        .set_series_opts(label_opts=opts.LabelOpts(formatter='{@[1]}m/s'))
        .set_global_opts(title_opts=opts.TitleOpts(title="当日风速变化"), yaxis_opts=opts.AxisOpts(
            type_='value',
            axislabel_opts=opts.LabelOpts(formatter="{value}m/s")))
    )
    return effectScatter


# 需求三：当日最高最低气温变化曲线
def high_low_temperature_line(hour, high_temperature, low_temperature) -> Line:
    line = (
        Line(init_opts=opts.InitOpts(width='50%'))
        .add_xaxis(xaxis_data=hour)
        .add_yaxis(
            series_name="白天气温",
            y_axis=high_temperature,
            markpoint_opts=opts.MarkPointOpts(
                data=[
                    opts.MarkPointItem(type_="max", name="最大值"),
                    opts.MarkPointItem(type_="min", name="最小值"),
                ]
            ),
            markline_opts=opts.MarkLineOpts(
                data=[opts.MarkLineItem(type_="average", name="平均值")]
            ),
        )
        .add_yaxis(
            series_name="夜晚气温",
            y_axis=low_temperature,
            markpoint_opts=opts.MarkPointOpts(
                data=[
                    opts.MarkPointItem(type_="max", name="最大值"),
                    opts.MarkPointItem(type_="min", name="最小值"),
                ]
            ),
            markline_opts=opts.MarkLineOpts(
                data=[opts.MarkLineItem(type_="average", name="平均值")]
            ),
        )
        .set_series_opts(label_opts=opts.LabelOpts(formatter='{@[1]}℃'))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="七日最高最低气温变化曲线"),
            tooltip_opts=opts.TooltipOpts(trigger="axis"),
            toolbox_opts=opts.ToolboxOpts(is_show=True),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
            yaxis_opts=opts.AxisOpts(
                type_='value',
                axislabel_opts=opts.LabelOpts(formatter="{value} ℃"))
        )
    )
    return line


# 需求四：当日气压
def pressure_gauge(hour, pressure) -> Timeline:
    tl = Timeline(init_opts=opts.InitOpts(width='50%'))
    for i in range(len(hour)):
        gauge = (
            Gauge()
            .set_global_opts(title_opts=opts.TitleOpts(title="气压仪表盘"),
                             legend_opts=opts.LegendOpts(is_show=False))
            .add(min_=0, max_=1500, data_pair=[(f'{hour[i]}时气压', pressure[i])], series_name=f'气压',
                 detail_label_opts=opts.GaugeDetailOpts(formatter="{value}hPa"))
        )
        tl.add(gauge, '{}'.format(hour[i]))
    return tl


# 需求五：当日云量散点图
def cloud_scatter(hour, cloud) -> Scatter:
    scatter = (
        Scatter(init_opts=opts.InitOpts(width='50%'))
        .add_xaxis(hour)
        .add_yaxis("云量", cloud)
        .set_series_opts(label_opts=opts.LabelOpts(formatter='{@[1]}%'))
        .set_global_opts(
            title_opts=opts.TitleOpts(title="当日云量散点图"),
            visualmap_opts=opts.VisualMapOpts(type_="size", max_=150, min_=20),
            yaxis_opts=opts.AxisOpts(
                type_='value',
                axislabel_opts=opts.LabelOpts(formatter="{value}%"))
        )
    )
    return scatter


# 需求六：七日天气预报表格
def seven_day_table(seven_day) -> Table:
    headers = ['日期', '星期', '白天天气', '白天风向', '白天风力', '白天气温', '晚上气温', '晚上天气', '晚上风向', '晚上风力']
    table = (
        Table()
        .add(headers, seven_day)
        .set_global_opts(
            title_opts=ComponentTitleOpts(title="七日天气预报")
        )
    )
    return table


# 需求汇总
def get_chart(city):
    # 数据获取
    seven_dayList, data_list, high_temperature, low_temperature, time_list, temperature_lists, \
    windspeed_lists, pressure_lists, humidity_lists, cloud_lists = weather_data(city)
    page = Page(layout=Page.SimplePageLayout)
    page.add(
        tab(f'{city}未来七日天气预报', '#000000'),
        temperature_line(time_list, temperature_lists),
        high_low_temperature_line(data_list, high_temperature, low_temperature),
        humidity_liquid(humidity_lists),
        pressure_gauge(time_list, pressure_lists),
        windspeed_effectScatter(time_list, windspeed_lists),
        cloud_scatter(time_list, cloud_lists),
        seven_day_table(seven_dayList)
    )
    # page.render("page_draggable_layout.html")
    return page


# def get_chart(city):
#     # 数据获取
#     seven_dayList, data_list, high_temperature, low_temperature, time_list, temperature_lists, \
#     windspeed_lists, pressure_lists, humidity_lists, cloud_lists = weather_data(city)
#     return temperature_line(time_list, temperature_lists), \
#            humidity_liquid(humidity_lists), \
#            windspeed_effectScatter(time_list, windspeed_lists), \
#            high_low_temperature_line(data_list, high_temperature, low_temperature), \
#            pressure_gauge(time_list, pressure_lists), \
#            cloud_scatter(time_list, cloud_lists), \
#            seven_day_table(seven_dayList)

if __name__ == '__main__':
    get_chart('衡阳市')
